using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

namespace ConsoleApp1
{
    public class TwoHavesTree<T> 
    {
        /// <summary>
        /// 树根
        /// </summary>
        public TreeNode<T> TreeRoot { get; set; }
         /// <summary>
 /// 先根遍历 
 /// </summary>
 /// <param name="root"></param>
 /// <returns></returns>
 public string RootFirst( TreeNode<T> root)
 {
     string content = "";
     if (root != null)
     {
         content += GetValue(root)+",";
     }
     if(root.Left!=null)
     {
         content += RootFirst(root.Left);
     }
     if(root.Right!=null)
     {
         content += RootFirst(root.Right);
     }
     return content;
 }
 /// <summary>
 /// 中根遍历 
 /// </summary>
 /// <param name="root"></param>
 /// <returns></returns>
 public string RootMid(TreeNode<T> root)
 {

     string content = "";
   
     if (root.Left != null)
     {
         content += RootMid(root.Left);
     }
     if (root != null)
     {
         content += GetValue(root) + ",";
     }
     if (root.Right != null)
     {
         content += RootMid(root.Right);
     }
     return content; 
 }
 /// <summary>
 /// 后根遍历 
 /// </summary>
 /// <param name="root"></param>
 /// <returns></returns>
 public string RootLast(TreeNode<T> root)
 {
     string content = "";

     if (root.Left != null)
     {
         content += RootLast(root.Left);
     }
    
     if (root.Right != null)
     {
         content += RootLast(root.Right);
     }
     if (root != null)
     {
         content += GetValue(root) + ",";
     }
     return content; 
 }
 private string GetValue(TreeNode<T> node)
 {
     return node.Value.ToString();
 }

        public void ShowAsHtml()
        {
            string filename = "index.html";
            string content= "<!DOCTYPE html>\r\n<html>\r\n\t<head>\r\n\t\t<meta charset=\"utf-8\" />\r\n\t\t<title></title>\r\n\t\t<script src=\"js/jquery-3.4.1.min.js\"></script>\r\n\t\t<style>\r\n\t\t\t* {\r\n\t\t\t\tmargin: auto;\r\n\t\t\t\tposition: relative;\r\n\t\t\t}\r\n\r\n\t\t\t.leaf {\r\n\t\t\t\ttext-align: center;\r\n\t\t\t\tmin-width: 49.5%;\r\n\t\t\t\twidth:fit-content;\r\n\r\n\t\t\t}\r\n\r\n\t\t\thr {}\r\n\r\n\t\t\t.left {\r\n\t\t\t\tfloat: left;\r\n\t\t\t\t}\r\n\r\n\t\t\t.root {\r\n\t\t\t\tborder-bottom: 1px red solid;margin: auto;width:50%;\r\n\t\t\t}\r\n\r\n\t\t\t.right {\r\n\t\t\t\tfloat: right;\r\n\t\t\t\t}\r\n\r\n\t\t\t.value {\r\n\t\t\t\twidth: fit-content;\r\n\t\t\t\tmin-width:15px;\r\n\t\t\t\tborder: 1px darkblue solid;\r\n\t\t\t\tpadding: 10px;\r\n\t\t\t\tborder-radius: 50%;\r\n\t\t\t\tmargin-bottom: 2px;\r\n\t\t\t\tmargin-top: 2px;\r\n\t\t\t}.noleft {\r\n\t\t\t\tborder-image: -webkit-linear-gradient(left, transparent 50%,red 50%, red 100%) 1;\r\n\t\t\t}\r\n\t\t\t.noright\r\n\t\t\t{\r\n\t\t\t\tborder-image: -webkit-linear-gradient(left, red 50%, transparent 50%, transparent 100%) 1;\r\n\t\t\t}\r\n\t\t</style>\r\n\t</head>\r\n\t<body>";
            content += GetDiv(TreeRoot);
            content += "</body></html>";
            File.WriteAllText(filename, content);
            Process.Start(filename);
        }
        private string  GetDiv(TreeNode<T> node,string dir=null)
        {
            string content = "<div class='leaf "+dir+"'><div class='root {root}'><div class='value'>" + node.Value+"</div></div>";
            if(node.Left==null&&node.Right==null)
            {
                content = content.Replace("{root}", "").Replace("root","");
                return content+"</div>";
            }
            if(node.Left!=null)
            {                
                content += GetDiv(node.Left,"left");
            }
            else
            {
                content = content.Replace("{root}", "noleft");
            }
            if(node.Right!=null)
            {
                content = content.Replace("{root}", "");
                content += GetDiv(node.Right,"right");
            }
            else
            {
                content = content.Replace("{root}", "noright");
            }
            return content+"</div>";
        }
    }

    public class TreeNode<T>
    {
        /// <summary>
        /// 右节点
        /// </summary>
        public TreeNode<T> Right
        {
            get
            { return _right; }
            set
            {
                _right = value;
                value.Parent = this;
            }
        }

        private TreeNode<T> _right { get; set; }
        /// <summary>
        /// 左节点
        /// </summary>
        public TreeNode<T> Left
        {
            get
            { return _left; }
            set
            {
                _left = value;
                value.Parent = this;
            }
        }

        private TreeNode<T> _left { get; set; }

        /// <summary>
        /// 获取根节点
        /// </summary>
        public TreeNode<T> Root
        {
            get
            {
                TreeNode<T> node = this;
                while (node.Parent != null)
                {
                    node = node.Parent;
                }
                return node;
            }

        }
        /// <summary>
        /// 父节点
        /// </summary>
        public TreeNode<T> Parent { get; private set; }
        /// <summary>
        /// 节点值 
        /// </summary>
        public T Value { get; set; }
        public TreeNode(T value)
        {
            Value = value;
        }

        public TreeNode()
        {

        }
        public void AddToLeft(TreeNode<T> node)
        {
            Left = node;
        }
        public void AddToRight(TreeNode<T> node)
        {
            Right = node;
        }
    }
}